<template>
  <div class="rightBodyContent">
    <div class="rightBodyContent-in">
      <tips optionTemplate="morningCheck"></tips>
      <div class="hearder-search">
        <div class="breadcrumb-container input-val-box mb-10">
          <component
            v-bind:is="item.propertyType"
            :property.sync="item"
            v-for="(item, index) in getPower"
            :key="index"
            @updateVal="updateVal"
          ></component>
          <el-date-picker
            v-model="monthValue"
            size="small"
            type="month"
            format="yyyyMM"
            value-format="yyyyMM"
            class="mr-10"
            placeholder="选择月份">
          </el-date-picker>
          <el-input v-model="editForm.name" placeholder="姓名" size="small"
                    class="w-140 mr-10"></el-input>
          <el-date-picker
            v-model="addtime"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            size="small"
            class="mr-10"
            :picker-options="pickerOptions1"
          >
          </el-date-picker>
          <el-select v-model="editForm.age_height" placeholder="身高指标" size="small"
                     class="w-140 mr-10">
            <el-option :label="item.name" :value="item.id" v-for="(item, index) in heightList"
                       :key="index"></el-option>
          </el-select>
          <el-select v-model="editForm.age_weight" placeholder="体重指标" size="small"
                     class="w-140 mr-10">
            <el-option :label="item.name" :value="item.id" v-for="(item, index) in weightList"
                       :key="index"></el-option>
          </el-select>
          <el-select v-model="editForm.obesity_type" placeholder="肥胖指标" size="small"
                     class="w-140 mr-10">
            <el-option :label="item.name" :value="item.id" v-for="(item, index) in weightType"
                       :key="index"></el-option>
          </el-select>
          <el-button type="primary" @click="searchBtn" size="small"><i class="el-icon-search"></i>搜索</el-button>
          <el-button size="small" @click="refreshFn"><i class="el-icon-refresh"></i>刷新</el-button>
        </div>
        <div class="mb-10 morningList">
          <el-button class="mr-10" size="small" @click="setTimeLine" :type="!timeStatus?'warning':'danger'" plain><i
            class="el-icon-download"></i>{{ timeStatus ? '取消自主刷新' : '自主刷新' }}
          </el-button>
          <el-upload
            :action="$uploadImg"
            :on-success="handleSuccess"
            :on-progress="handleProgress"
            :show-file-list="false"
            accept=".xls,.xlsx"
            class="el-upload-body mr-10">
            <el-button type="primary" size="small">导入班级体检发育模版</el-button>
          </el-upload>
          <downexcel type="growthClass" :option="{'class_id': editForm.class_id,'month': monthValue}" v-if="editForm.class_id && monthValue"></downexcel>
          <el-button size="small" :disabled="true" v-else>导出班级体检发育模版</el-button>
          <downexcel type="growth" :option="editForm"></downexcel>
          <!--          <span class="ml-10"><i class="el-icon-warning-outline mr-5"-->
          <!--                                 style="color:#eca32e"></i>评价标准只适用于标准范围内，超出范围不予评价</span>-->
          <el-button size="small" @click="refreshEditFn" type="primary" plain><i class="el-icon-refresh"></i>重置体检报告
          </el-button>
        </div>
        <div class="mb-10 morningList">
          <el-button icon="el-icon-warning-outline" size="small" @click="standarFn(1)">年龄身高标准</el-button>
          <el-button icon="el-icon-warning-outline" size="small" @click="standarFn(2)">年龄体重标准</el-button>
          <el-button icon="el-icon-warning-outline" size="small" @click="standarFn(3)">身高对应体重标准</el-button>
          <el-button icon="el-icon-warning-outline" size="small" @click="standarFn(4)">营养评价标准</el-button>
          <!--          <span class="ml-10"><i class="el-icon-warning-outline mr-5"-->
          <!--                                 style="color:#eca32e"></i>评价标准只适用于标准范围内，超出范围不予评价</span>-->
          <span class="allbook">总 {{ page.total }} 条数据</span>
        </div>
      </div>
      <!--本体列表，数据 -->
      <el-table
        ref="multipleTable"
        :data="arryList"
        :stripe="tableStripe"
        :border="tableBorder"
        tooltip-effect="dark"
        header-cell-class-name="table-text-c"
        cell-class-name="table-text-c">
        <el-table-column
          align="center"
          prop="id"
          label="ID"
          width="80">
        </el-table-column>
        <el-table-column
          align="center"
          label="学号"
          width="120">
          <template slot-scope="scope">
            {{ scope.row.student ? scope.row.student.student_code : '' }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="name"
          label="姓名">
          <template slot-scope="scope">
            {{ scope.row.student ? scope.row.student.name : '' }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="catname"
          label="班级"
          width="140">
          <template slot-scope="scope">
            {{ scope.row.classes ? scope.row.classes.class_name : '' }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="月龄"
          width="140">
          <template slot-scope="scope">
            <span v-if="scope.row.year">{{ scope.row.year ? scope.row.year : '' }}</span>
            <el-link type="danger" v-else>出生年月格式错误</el-link>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="editor"
          label="身高(cm)/评价">
          <template slot-scope="scope">
            {{ scope.row.height }}/{{ scope.row.age_height }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="position"
          label="体重(kg)/评价">
          <template slot-scope="scope">
            {{ scope.row.weight }}/{{ scope.row.age_weight }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="position"
          label="营养评价">
          <template slot-scope="scope">
            {{ scope.row.obesity_type }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="create_time"
          label="采集时间">
        </el-table-column>
        <el-table-column
          align="center"
          width="160px"
          label="操作">
          <template slot-scope="scope">
            <el-button plain size="small" @click="updateEditFn(scope.row)">编辑</el-button>
            <el-button plain type="danger" size="small" @click="delBtn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--工具条 分页-->
      <el-col :span="24" class="toolbar">
        <el-pagination
          background
          :page-size="page.pagesize"
          :current-page="page.pageindex"
          layout="total, prev, pager, next"
          :total="page.total"
          @current-change="handleSizeChange"></el-pagination>
      </el-col>
    </div>
    <div class="height40"></div>
    <standardList :visible.sync="standardvisible" :standardtype="standardtype"></standardList>
    <updateData :visible.sync="updatevisible" :dataOption="dataOption" @InitFn="InitFn"></updateData>
    <resetData :visible.sync="resetvisible" @InitFn="InitFn"></resetData>
  </div>
</template>

<script>
import {pagination} from '@/utils/util.js'
import standardList from "@/components/standard/standardList";
import {growthIndex, growthImporant} from "@/api/admin/medicalExam"
import updateData from "./components/updateData";
import resetData from "./components/resetData";
import {dstudent_growthDelete} from '@/api/admin/development'

export default {
  components: {
    standardList,
    updateData,
    resetData
  },
  data() {
    return {
      tableStripe: true, // 是否斑马栏
      tableBorder: true, // 单元格右边边框
      updatevisible: false,
      resetvisible: false,
      dataOption: null,
      arryList: [], // 数据列表
      handleSelection: [], // checked选中框
      pickerOptions1: {
        disabledDate(time) {
          // return time.getTime() > Date.now() - 8.64e7 // 不包括今天
          return time.getTime() > Date.now()
        }
      },
      monthValue:'',
      // 页数
      page: {
        pageindex: pagination.pageIndex,
        pagesize: pagination.pageSize,
        total: pagination.total
      },
      // 添加图书弹窗参数
      editForm: {
        name: '',
        start_time: '',
        end_time: '',
        age_height: '',
        age_weight: '',
        class_id: '',
        section_id: '',
        obesity_type: ''
      },
      addtime: [],
      standardvisible: false,
      standardtype: 1,
      heightLine: '',
      getPower: {
        'section_id': {
          name: 'section_id', // key名称
          default: '', // 默认值
          propertyType: "Select", // 组件名称
          placeholder: '年段', // 默认值
          valueText: '', // 值
          leve: 2,
          api: this.api.user_sectionindex, // 下拉框的请求接口
          keyName: 'section_name',// 调用的字段
          child: [
            {
              name: 'class_id',
              default: '',
              propertyType: "",
              placeholder: '班级',
              valueText: '',
              api: this.api.user_classesindex,
              keyName: 'class_name',
            }
          ]
        }
      },
      heightList: [{
        id: 1,
        name: '下'
      },
        {
          id: 2,
          name: '中下'
        },
        {
          id: 3,
          name: '中-'
        },
        {
          id: 4,
          name: '中'
        },
        {
          id: 5,
          name: '中+'
        },
        {
          id: 6,
          name: '中上'
        },
        {
          id: 7,
          name: '上'
        }],
      weightLine: '',
      weightList: [{
        id: 1,
        name: '下'
      },
        {
          id: 2,
          name: '中下'
        },
        {
          id: 3,
          name: '中-'
        },
        {
          id: 4,
          name: '中'
        },
        {
          id: 5,
          name: '中+'
        },
        {
          id: 6,
          name: '中上'
        },
        {
          id: 7,
          name: '上'
        }],
      weightType: [
        {
          id: 1,
          name: '超重'
        },
        {
          id: 2,
          name: '轻度肥胖'
        },
        {
          id: 3,
          name: '中度肥胖'
        },
        {
          id: 4,
          name: '重度肥胖'
        }
      ],
      timeNumberFn: null,
      timeStatus: false
    }
  },
  created() {
    this.InitFn()
  },
  methods: {
    // 初始化
    InitFn() {
      this.editForm.p = this.page.pageindex
      console.log('时间:', this.addtime)
      if (this.addtime.length) {
        this.editForm.start_time = this.addtime[0]
        this.editForm.end_time = this.addtime[1]
      } else {
        this.editForm.start_time = ''
        this.editForm.end_time = ''
      }
      this.$fetch(this.api.Developmental.morningCheck.index, this.editForm).then((response) => {
        if (response.code === 0) {
          // 37.5算发烧
          for (let i in response.list.data) {
            response.list.data[i].create_time = response.list.data[i].create_time.split(' ')[0]
            if (Number(response.list.data[i].temperature) < 37.5) {
              response.list.data[i].type = 1
            } else {
              response.list.data[i].type = 2 // 发烧了
            }
          }
          this.arryList = response.list.data
          this.page.total = response.list.total
          console.log('接收到的数据信息', this.arryList, this.page.total)
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    // 计时刷新列表
    getTimeList() {
      growthIndex({p: this.page.pageindex}).then(res => {
        if (!res.code) {
          // 37.5算发烧
          for (let i in res.list.data) {
            res.list.data[i].create_time = res.list.data[i].create_time.split(' ')[0]
            if (Number(res.list.data[i].temperature) < 37.5) {
              res.list.data[i].type = 1
            } else {
              res.list.data[i].type = 2 // 发烧了
            }
          }
          this.arryList = res.list.data
          this.page.total = res.list.total
        }
      })
    },
    // 开始计时
    setTimeLine() {
      let that = this;
      if (!this.timeStatus) {
        this.clearTimeFn()
        this.timeStatus = true
        this.editForm.name = ''
        this.page.pageindex = 1
        this.editForm.start_time = ''
        this.editForm.end_time = ''
        this.editForm.age_height = ''
        this.editForm.age_weight = ''
        this.addtime = []
        let newGetPower = this.getJson(this.getPower)
        newGetPower['section_id'].valueText = '';
        newGetPower['section_id'].child[0].valueText = '';
        this.getPower = newGetPower
        this.timeNumberFn = setInterval(() => {
          that.getTimeList()
        }, 1000)
      } else {
        this.timeStatus = false
        clearInterval(this.timeNumberFn)
      }
    },
    clearTimeFn() {
      this.timeStatus = false
      clearInterval(this.timeNumberFn)
    },
    // 分页
    handleSizeChange(val) {
      this.clearTimeFn()
      this.page.pageindex = val
      this.InitFn()
    },
    handleSelectionChange(val) {
      this.handleSelection = val
    },
    // 搜索
    searchBtn() {
        this.clearTimeFn()
        this.page.pageindex = 1
        // 添加对 monthValue 和 addtime 的处理
        this.editForm.month = this.monthValue
        if (this.addtime.length) {
            this.editForm.start_time = this.addtime[0]
            this.editForm.end_time = this.addtime[1]
        } else {
            this.editForm.start_time = ''
            this.editForm.end_time = ''
        }
        this.InitFn()
    },
    // 刷新页面
    refreshFn() {
      this.clearTimeFn()
      this.editForm.name = ''
      this.page.pageindex = 1
      this.editForm.start_time = ''
      this.editForm.end_time = ''
      this.editForm.age_height = ''
      this.editForm.age_weight = ''
      this.editForm.obesity_type = ''
      this.addtime = []
      let newGetPower = this.getJson(this.getPower)
      newGetPower['section_id'].valueText = '';
      newGetPower['section_id'].child[0].valueText = '';
      this.getPower = newGetPower
      this.monthValue = ''
      this.InitFn()
    },
    standarFn(type) {
      this.standardtype = type
      this.standardvisible = true
    },
    updateVal(option) {
      // console.log('变更数据收到的数据内容：', option)
      if (option.input) {
        this.editForm[option['input'].name] = option['input'].valueText
      } else if (option.select) {
        for (let i in option.select) {
          if (option.select[i].name) {
            this.editForm[option.select[i].name] = option.select[i].valueText
          } else {
            this.editForm[option.select[i - 1].name] = option.select[i].valueText ? option.select[i].valueText : option.select[i - 1].valueText
          }
        }
      }
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    // 重置体检报告
    refreshEditFn() {
      this.clearTimeFn()
      this.editForm.name = ''
      this.page.pageindex = 1
      this.editForm.start_time = ''
      this.editForm.end_time = ''
      this.editForm.age_height = ''
      this.editForm.age_weight = ''
      this.addtime = []
      let newGetPower = this.getJson(this.getPower)
      newGetPower['section_id'].valueText = '';
      newGetPower['section_id'].child[0].valueText = '';
      this.getPower = newGetPower
      this.resetvisible = true
    },
    // 更新体检报告单个学生
    updateEditFn(row) {
      this.dataOption = JSON.parse(JSON.stringify(row))
      this.updatevisible = true
    },
    handleProgress() {
      this.progressPercent = parseInt(event.percent)
    },
    handleSuccess(response) {
      this.materielImport(response.data.name)
    },
    // 导入物料
    async materielImport(path) {
      growthImporant({file: path}).then(res => {
        if (!res.code) {
          this.$message.success(res.msg)
          this.InitFn()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    delBtn(row) {
      let _this = this
      this.$confirm('是否确认删除此数据', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return dstudent_growthDelete({id: row.id})
      }).then(data => {
        _this.$message({
          showClose: true,
          message: '删除成功',
          type: 'success'
        })
        _this.InitFn()
      }).catch(function (err) {
      })
    }
  },
  mounted() {
  },
  // 离开当前页面后执行
  destroyed: function () {
    this.timeStatus = false
    clearInterval(this.timeNumberFn)
  }
}
</script>
<style>
.morningList {
  display: flex;
  align-items: center;
}
</style>

